package com.exm.collectcodenew.activemq.produce;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class ActiveMQTopicProducePersist {
    public static final String ACTIVEMQ_URL ="tcp://59.110.35.177:61616";
    public static final String TOPIC_NAME="topic-persist";

    public static void main(String[] args) throws JMSException {
        //1.创建连接工厂，按照给定的url地址，采用默认用户名和密码
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        //2.根据连接工厂创建连接
        Connection connection=activeMQConnectionFactory.createConnection();
        //3.创建session  两个参数，第一个叫事务/第二个叫签收
        Session session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        //4.创建目的地（队列/topic）
        Topic topic = session.createTopic(TOPIC_NAME);
        //5.创建生产者
        MessageProducer messageProducer = session.createProducer(topic);
        //6.设置持久模式
        messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
        //7.启动连接
        connection.start();
        for (int i = 0; i < 3; i++) {
            //8.创建消息
            TextMessage textMessage = session.createTextMessage("msg-persist-"+i);
            //9.发送消息
            messageProducer.send(textMessage);
        }
        //10.关闭资源
        messageProducer.close();
        session.close();
        connection.close();
    }
}
